<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>新增或编辑</title>
</head>

<body>
    <h1>添加博客或者修改</h1>
    <table>
        
        <tr>
            <td><label for="">标题:</label></td>
            <td><input type="text" placeholder="请输入标题" name="biaoti"></td>
        </tr>
        <tr>
            <td><label for="">摘要:</label></td>
            <td><input type="text" placeholder="请输入摘要" name="zaiyao"></td>
        </tr>
        <tr>
            <td><label for="">内容:</label></td>
            <td><input type="text" placeholder="请输入内容" name="neiron"></td>
        </tr>
        <tr>
            <td><label for="">分类:</label></td>
            <td><input type="text" placeholder="请输入分类" name="fenlei"></td>
        </tr>
        <tr>
            <td><label for="">作者:</label></td>
            <td><input type="text" placeholder="请输入作者" name="zuoze"></td>
        </tr>
        <tr>
            <td>
                <input type="button" value="保存" onclick="save()">
                <input type="button" value="取消" onclick="cancel()">
            </td>
        </tr>
    </table>
    <script src="./js/jquery-3.6.0.min.js"></script>
    <script src="./config/baseUrl.js"></script>
    <script src="js/api.js"></script>
   
    <script>
        $(function () {
            let id = getUrlParm('id');//调用
            console.log('Url的id为：'+id);

            id=id || 0 ;//当id为空时,0
            getbokeByid(id).then(res => {
                console.log(res);
                //res传过来的是一个对象
                let obj = res.data;//拿到后端根据id找出的商品


                //id为空时，赋的值也为空
                $('[name=biaoti]').val(obj.biaoti);
                $('[name=zaiyao]').val(obj.zaiyao);
                $('[name=neiron]').val(obj.neiron);
                $('[name=fenlei]').val(obj.fenlei);
                $('[name=zuoze]').val(obj.zuoze);
            })
        })

        //当参数值为id时判断url是否有传入id的值，有则返回，没有则返回null
        function getUrlParm(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        }

        function save() {
            let id = getUrlParm('id');
            console.log(id);
            let obj = {
                biaoti: $('[name=biaoti]').val(),
                zaiyao: $('[name=zaiyao]').val(),
                neiron: $('[name=neiron]').val(),
                fenlei: $('[name=fenlei]').val(),
                zuoze: $('[name=zuoze]').val(),
            }

            if (id) {
                updateboke(id, obj).then(res => {
                    console.log(res);
                    location.href = './index.html'
                })
            } else {
                let params = {
                    biaoti: obj.biaoti,
                    zaiyao:obj.zaiyao,
                    neiron:obj.neiron,
                    fenlei:obj.fenlei,
                    zuoze:obj.zuoze
                }
                console.log(params);
                addboke(params).then(res => {
                    location.href = './index.html';
                })
            }
        }


        //取消并返回
        function cancel() {
            location.href = './index.html'
        }
    </script>

</body>

</html>